/**
 * 👨‍💻 伟谭
 * 📅 2017/11/25
 */

#import "LXHLongXiangHuiHomelayoutJsonModel.h"

#import "ULBCollectionViewFlowLayout.h"

#import "TWConvenienceServiceDetailsViewController.h"



@interface TWConvenienceServiceDetailsViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong,readonly) UICollectionView *mainCollectionView;

@property (nonatomic,strong,readonly)LXHLongXiangHuiHomelayoutJsonModel * longXiangHuiHomelayoutModelData;

@end

// 板块广告
typedef enum : NSUInteger {
    
    convenienceServiceDetailTypeOfProfile = 0,//便民服务简介 1*1
    convenienceServiceDetailTypeOfAlbum,//相册
    convenienceServiceDetailTypeOfToProvideServices,//提供服务
    
    
} convenienceServiceDetailType;

@implementation TWConvenienceServiceDetailsViewController

#pragma mark - Class method

#pragma mark - Life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor hj_colorWithRGBHex:0x00000f] ; [UIColor hj_colorWithRGBHex:0xefefef];
    
    [self initializeViews];
}

#pragma mark - Override

#pragma mark - Event response

#pragma mark - Protocol

#pragma mark <<#XXXDelegate#>>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[indexPath.section];
    
    return layoutGroupModel.bodyLayout.itemSize;
    
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.bodyLayout.topLeftBottonRightSpaceEdge;
    
    
}

/*
 * 水平间距
 **/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.bodyLayout.levelOfspace;
    
}

/*
 * 垂直间距
 **/
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.bodyLayout.verticalSpace;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.headerLayout.headerSize;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.footerLayout.footerSize;
}

#pragma mark <UICollectionViewDataSource>

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.longXiangHuiHomelayoutModelData.numberOfSections;
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[section];
    return layoutGroupModel.bodyLayout.numberOfItemsTotal;
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[indexPath.section];
    
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: layoutGroupModel.bodyLayout.Identifier forIndexPath:indexPath];
    
    return cell;
    
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    LXHLongXiangHuiHomelayoutGroupJsonModel * layoutGroupModel = self.longXiangHuiHomelayoutModelData.layoutData[indexPath.section];
    if(kind == UICollectionElementKindSectionHeader){
        UICollectionReusableView *heaerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:layoutGroupModel.headerLayout.Identifier forIndexPath:indexPath ];
        return heaerView;
    }else if(kind == UICollectionElementKindSectionFooter){
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:layoutGroupModel.footerLayout.Identifier forIndexPath:indexPath ];
        return footerView;
    }
    return nil;
}

//添加白色块背景
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    if(section == convenienceServiceDetailTypeOfToProvideServices){
        return [UIColor whiteColor];
    }
    return nil;
}

#pragma mark - Public method

#pragma mark - Private method
- (void)initializeViews {
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"LongXiangHuiHomelayoutList" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSError *error;
    LXHLongXiangHuiHomelayoutJsonModel * longXiangHuiHomelayoutModel =  [[LXHLongXiangHuiHomelayoutJsonModel alloc] initWithDictionary:dictionary error:&error];
    if(error){
        NSAssert(@"解密失败 %@",error.localizedDescription);
    }else{
        _longXiangHuiHomelayoutModelData = longXiangHuiHomelayoutModel;
    }
    
    
    
   // UICollectionViewFlowLayout * collectionViewFlowLayout = [UICollectionViewFlowLayout new];
    
    // 流水布局
    ULBCollectionViewFlowLayout *collectionViewFlowLayout = [[ULBCollectionViewFlowLayout alloc] init];
    
    UICollectionView *mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewFlowLayout];
    mainCollectionView.showsVerticalScrollIndicator = NO;
    mainCollectionView.backgroundColor = [UIColor grayColor];
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
    [self.view addSubview:mainCollectionView];
    _mainCollectionView = mainCollectionView;
    
    [mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0.0f);
    }];
    
    [self.longXiangHuiHomelayoutModelData.layoutData enumerateObjectsUsingBlock:^(LXHLongXiangHuiHomelayoutGroupJsonModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        
        [mainCollectionView registerClass:NSClassFromString(obj.bodyLayout.className) forCellWithReuseIdentifier:obj.bodyLayout.Identifier];
        
       /*
        if([obj.bodyLayout.className isEqualToString:NSStringFromClass([LXHCompositePlateCollectionViewCell class])]){
        [LXHDragonTownshipWouldKillCollectionViewCell setHeaderHeight:30.0f];
        }
        */
        if(obj.headerLayout){
        [mainCollectionView registerClass:NSClassFromString(obj.headerLayout.className) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:obj.headerLayout.Identifier];
        }
        
        if(obj.footerLayout){
        [mainCollectionView registerClass:NSClassFromString(obj.footerLayout.className) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:obj.footerLayout.Identifier];
        }
        
        
        
    }];
    
    
   
    
 
    
}

#pragma mark - Getter & Setter

@end
